<template>
  <el-dialog
    :model-value="modelValue"
    @update:model-value="updateVisible"
    :close-on-click-modal="false"
    :title="button"
  >
    <el-form
      :model="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="120px"
    >
      <el-form-item label="活动场次id" prop="promotionSessionId">
        <el-input
          v-model="props.sessionId"
          placeholder="活动场次id"
          :disabled="true"
        ></el-input>
      </el-form-item>
      <el-form-item label="商品id" prop="skuId">
        <el-input v-model="dataForm.skuId" placeholder="商品id"></el-input>
      </el-form-item>
      <el-form-item label="秒杀价格" prop="seckillPrice">
        <el-input-number
          v-model="dataForm.seckillPrice"
          :min="0"
          :precision="2"
          :step="0.1"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="秒杀总量" prop="seckillCount">
        <el-input-number
          v-model="dataForm.seckillCount"
          :min="1"
          label="秒杀总量"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="每人限购数量" prop="seckillLimit">
        <el-input-number
          v-model="dataForm.seckillLimit"
          :min="1"
          label="每人限购数量"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="排序" prop="seckillSort">
        <el-input v-model="dataForm.seckillSort" placeholder="排序"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleCancel">取消</el-button>
        <el-button type="primary" @click="dataFormSubmit()">
          {{ button }}
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { nextTick, ref, defineExpose } from 'vue'
import { ElMessage } from 'element-plus'
import {
  reqSeckillSkuRelationInfo,
  reqSeckillSkuRelationUpdateCommit,
} from '@/api/coupon/seckillsession'

const dataForm = ref({
  id: 0,
  promotionId: '',
  promotionSessionId: '',
  skuId: '',
  seckillPrice: 0,
  seckillCount: 0,
  seckillLimit: 1,
  seckillSort: 0,
})
const button = ref('添加')
const visible = ref(false)

const props = defineProps(['modelValue', 'sessionId'])

const emit = defineEmits(['update:modelValue'])

const updateVisible = (value: boolean) => {
  emit('update:modelValue', value)
}

// 处理取消按钮点击
const handleCancel = () => {
  updateVisible(false)
}

const dataFormSubmit = async () => {
  try {
    await reqSeckillSkuRelationUpdateCommit(
      dataForm.value.id > 0 ? 'update' : 'save',
      dataForm.value.id,
      dataForm.value.promotionId,
      props.sessionId,
      dataForm.value.skuId,
      dataForm.value.seckillPrice,
      dataForm.value.seckillCount,
      dataForm.value.seckillLimit,
      dataForm.value.seckillSort,
    )
    ElMessage({
      type: 'success',
      message: `秒杀商品${button.value}成功`,
      duration: 1500,
      onClose: () => {
        visible.value = false
      },
    })
  } catch (error) {
    console.error(`秒杀商品${button.value}失败`, error)
  }
}

const init = (id: number) => {
  button.value = id > 0 ? '修改' : '添加'
  nextTick(async () => {
    dataForm.value.id = 0
    dataForm.value.promotionId = ''
    dataForm.value.promotionSessionId = ''
    dataForm.value.skuId = ''
    dataForm.value.seckillPrice = 0
    dataForm.value.seckillCount = 0
    dataForm.value.seckillLimit = 1
    dataForm.value.seckillSort = 0
    if (id > 0) {
      try {
        const response = await reqSeckillSkuRelationInfo(id)
        if (response.code === 0) {
          dataForm.value.id = response.seckillSkuRelation.id
          dataForm.value.promotionId = response.seckillSkuRelation.promotionId
          dataForm.value.promotionSessionId =
            response.seckillSkuRelation.promotionSessionId
          dataForm.value.skuId = response.seckillSkuRelation.skuId
          dataForm.value.seckillPrice = response.seckillSkuRelation.seckillPrice
          dataForm.value.seckillCount = response.seckillSkuRelation.seckillCount
          dataForm.value.seckillLimit = response.seckillSkuRelation.seckillLimit
          dataForm.value.seckillSort = response.seckillSkuRelation.seckillSort
        }
      } catch (error) {
        console.error('秒杀商品信息获取失败:', error)
      }
    }
  })
}

// 暴露方法
defineExpose({ init })
</script>

<style scoped lang="scss"></style>
